﻿using SlamDunk.CMS.DAL;
using SlamDunk.CMS.Model.DataModel;
using System;
using System.Collections.Generic;

namespace SlamDunk.CMS.BLL
{
    public class PhotoBll
    {
        #region 01.实例化数据层
        /// <summary>
        /// 实例化数据层
        /// </summary>
        private readonly PhotoDal CurrentDal = new PhotoDal();
        #endregion

        #region 02.添加
        /// <summary>
        /// 添加一个实体对象
        /// </summary>
        /// <param name="photo">构造好要添加的实体对象</param>
        /// <returns>添加完毕后的实体对象</returns>
        public Photo Add(Photo photo)
        {
            return CurrentDal.Add(photo);
        }
        #endregion

        #region 03.删除（物理删除）
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <param name="id">要删除的实体对象Id</param>
        /// <returns>删除结果</returns>
        public bool PhysicalDelete(int id)
        {
            return CurrentDal.PhysicalDelete(id) > 0;
        }
        #endregion

        #region 05.修改
        /// <summary>
        /// 修改一个实体对象
        /// </summary>
        /// <param name="photo">构造好的要修改的实体对象</param>
        /// <returns>修改结果</returns>
        public bool Update(Photo photo)
        {
            return CurrentDal.Update(photo) > 0;
        }
        #endregion

        #region 06.根据主键获取单个实体对象
        /// <summary>
        /// 根据主键获取单个实体对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>单个实体对象</returns>
        public Photo GetModel(int id)
        {
            return CurrentDal.GetModel(id);
        }
        #endregion

        #region 03.根据相册Id查询图片记录条数
        /// <summary>
        /// 根据相册Id查询图片记录条数
        /// </summary>
        /// <param name="albumId">相册Id</param>
        /// <returns>符合条件的图片记录条数</returns>
        public int GetCount(int albumId)
        {
            return this.CurrentDal.GetCount(albumId);
        }
        #endregion

        #region 04.根据相册Id分页查询图片列表
        /// <summary>
        /// 根据相册Id分页查询图片列表
        /// </summary>
        /// <param name="start">起始行</param>
        /// <param name="end">结束行</param>
        /// <param name="albumId">相册Id</param>
        /// <returns>图片列表</returns>
        public List<Photo> GetModelList(int pageIndex, int pageSize, out int pageCount, out int rowCount, int albumId)
        {
            //1.计算总记录条数
            rowCount = this.GetCount(albumId);
            pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
            //2.页索引处理
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
            //3.计算起始行与终止行
            int start = (pageIndex - 1) * pageSize + 1;
            int end = pageIndex * pageSize;
            return this.CurrentDal.GetModelList(start, end, albumId);
        }
        #endregion

        #region 05.根据相册Id查询图片列表
        /// <summary>
        /// 根据相册Id查询图片列表
        /// </summary>
        /// <param name="albumId">相册Id</param>
        /// <returns>图片列表</returns>
        public List<Photo> GetModelList(int albumId)
        {
            return this.CurrentDal.GetModelList(albumId);
        }
        #endregion
    }
}
